﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UTIL
{
    public static class Xml
    {
        #region Read DvdId Xml
        public static string ReadDvdId(string DvdIdFilePath)
        {
            string dvdId = string.Empty;

            System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
            xmlDocument.Load(DvdIdFilePath);

            System.Xml.XmlNode xmlNode = xmlDocument.FirstChild;
            xmlNode = xmlNode.NextSibling;

            foreach (System.Xml.XmlNode childXmlNode in xmlNode.ChildNodes)
            {
                if (childXmlNode.Name == "ID")
                {
                    dvdId = childXmlNode.InnerText;
                }
            }

            return dvdId;
        }
        #endregion

        #region Write DVDID Xml
        public static bool WriteDVDIDXml(string Title, string DVDID, string Location)
        {
            bool success = false;

            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", string.Empty, string.Empty);
            xmlDoc.AppendChild(xmlDeclaration);

            System.Xml.XmlNode discNode = xmlDoc.CreateNode(System.Xml.XmlNodeType.Element, "Disc", "");
            xmlDoc.AppendChild(discNode);

            System.Xml.XmlNode nameNode = xmlDoc.CreateNode(System.Xml.XmlNodeType.Element, "Name", "");
            nameNode.InnerText = Title;
            discNode.AppendChild(nameNode);

            System.Xml.XmlNode idNode = xmlDoc.CreateNode(System.Xml.XmlNodeType.Element, "ID", "");
            idNode.InnerText = DVDID;
            discNode.AppendChild(idNode);

            try
            {
                xmlDoc.Save(Location);
                success = true;
            }
            catch (Exception ex)
            {

            }

            return success;
        }
        #endregion

        #region Read DvdInfoCache Xml
        public static void ReadDvdInfoCacheXml(ref BLL.InfoCache InfoCache)
        {
            if (InfoCache == null)
            {
                throw new ApplicationException("InfoCache is Null");
            }

            InfoCache.DvdInfoCacheXmlDocument = new System.Xml.XmlDocument();
            InfoCache.DvdInfoCacheXmlDocument.Load(InfoCache.DvdInfoCacheFilePath);

            // Xml Declaration
            System.Xml.XmlNode xmlNode = InfoCache.DvdInfoCacheXmlDocument.FirstChild;

            // METADATA Tag
            xmlNode = xmlNode.NextSibling;

            // MDR-DVD Tag
            xmlNode = xmlNode.FirstChild;

            foreach (System.Xml.XmlNode childXmlNode in xmlNode.ChildNodes)
            {
                switch (childXmlNode.Name)
                {
                    case "dvdTitle":
                        InfoCache.Title = childXmlNode.InnerText;
                        break;
                    case "largeCoverParams":
                        if (childXmlNode.InnerText.Length > 0)
                        {
                            InfoCache.IsLargeCoverParamsSet = true;
                        }
                        break;
                    case "smallCoverParams":
                        if (childXmlNode.InnerText.Length > 0)
                        {
                            InfoCache.IsSmallCoverParamsSet = true;
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        #endregion

        #region Edit DvdInfoCache Xml
        public static void EditDvdInfoCacheXml(BLL.InfoCache InfoCache, string Title, bool RemoveLargeCoverParams, bool RemoveSmallCoverParams)
        {
            if (InfoCache == null)
            {
                throw new ApplicationException("InfoCache is Null");
            }

            InfoCache.DvdInfoCacheXmlDocument = new System.Xml.XmlDocument();
            InfoCache.DvdInfoCacheXmlDocument.Load(InfoCache.DvdInfoCacheFilePath);

            // Xml Declaration
            System.Xml.XmlNode xmlNode = InfoCache.DvdInfoCacheXmlDocument.FirstChild;

            // METADATA Tag
            xmlNode = xmlNode.NextSibling;

            // MDR-DVD Tag
            xmlNode = xmlNode.FirstChild;

            System.Xml.XmlNode largeCoverParamsNode = null;
            System.Xml.XmlNode smallCoverParamsNode = null;
            foreach (System.Xml.XmlNode childXmlNode in xmlNode.ChildNodes)
            {
                switch (childXmlNode.Name)
                {
                    case "dvdTitle":
                        childXmlNode.InnerText = Title;
                        break;
                    case "largeCoverParams":
                        if (RemoveLargeCoverParams)
                        {
                            largeCoverParamsNode = childXmlNode;
                        }
                        break;
                    case "smallCoverParams":
                        if (RemoveSmallCoverParams)
                        {
                            smallCoverParamsNode = childXmlNode;
                        }
                        break;
                    default:
                        break;
                }
            }

            if (largeCoverParamsNode != null)
            {
                xmlNode.RemoveChild(largeCoverParamsNode);
            }

            if (smallCoverParamsNode != null)
            {
                xmlNode.RemoveChild(smallCoverParamsNode);
            }

            InfoCache.DvdInfoCacheXmlDocument.Save(InfoCache.DvdInfoCacheFilePath);
        }
        #endregion
    }
}
